# SPDX-FileCopyrightText: 2025 MounRiver Studio <https://mounriverstudio.com/>
# SPDX-License-Identifier: BSD-3-Clause
# Author: Azolla <https://gitee.com/azolla/>
# Date: 2025-04-21
if(NOT DEFINED TOOLCHAIN_IS_INCLUDED)
set(TOOLCHAIN_IS_INCLUDED     TRUE)
set(CMAKE_SYSTEM_NAME         Linux) # 选择目标系统 Linux / Windows
set(CMAKE_SYSTEM_PROCESSOR    riscv) # 默认目标处理器 arm / riscv
set(CMAKE_SYSTEM_VERSION      3.26)  # 默认目标版本

# 设置交叉编译器
set(CMAKE_C_COMPILER_FORCED   TRUE)  # 强制使用C交叉编译器
set(CMAKE_CXX_COMPILER_FORCED TRUE)  # 强制使用CXX交叉编译器
set(CMAKE_ASM_COMPILER_FORCED TRUE)  # 强制使用ASM交叉编译器
set(CMAKE_C_COMPILER_ID       GNU)   # 设置C交叉编译器为   GNU / GCC
set(CMAKE_CXX_COMPILER_ID     GNU)   # 设置CXX交叉编译器为 GNU / G++
set(CMAKE_ASM_COMPILER_ID     GNU)   # 设置ASM交叉编译器为 GNU / GCC

# Setup compiler settings
set(CMAKE_C_STANDARD          11)    # C11
set(CMAKE_C_STANDARD_REQUIRED ON)    # C11 required
set(CMAKE_C_EXTENSIONS        ON)    # C11 extensions enabled
 
# 指定交叉编译器
#set(MRS_TOOLCHAIN_PATH  "C:/MounRiver/MounRiver_Studio/toolchain")
# PowerShell(管理员)查看：echo $env:MRS_TOOLCHAIN_PATH
# Linux: echo $MRS_TOOLCHAIN_PATH
if(UNIX)
    # 在 Linux 系统上
    # message(STATUS "${G}Building on Linux${E}")
    # 设置 Linux 交叉编译器路径
    set(CMAKE_ASM_COMPILER   "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-gcc")
    set(CMAKE_C_COMPILER     "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-gcc")
    set(CMAKE_CXX_COMPILER   "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-g++")
    set(CMAKE_LINKER         "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-ld")
    set(CMAKE_SIZE           "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-size")
    set(CMAKE_OBJCOPY        "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objcopy")
    set(CMAKE_OBJDUMP        "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objdump")
    set(RISCV_SIZE           "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-size")
    set(RISCV_OBJCOPY        "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objcopy")
    set(RISCV_OBJDUMP        "${MRS_TOOLCHAIN_PATH}/Toolchain/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objdump")
    set(RISCV_OPENOCD        "${MRS_TOOLCHAIN_PATH}/OpenOCD/OpenOCD/bin/openocd")
    set(RISCV_OPENOCD_CONFIG "${MRS_TOOLCHAIN_PATH}/OpenOCD/OpenOCD/bin/wch-riscv.cfg")
elseif(WIN32)
    # 在 Windows 系统上
    # message(STATUS "${G}Building on Windows${E}")
    # 设置 Windows 交叉编译器路径
    set(CMAKE_ASM_COMPILER   "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-gcc.exe")
    set(CMAKE_C_COMPILER     "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-gcc.exe")
    set(CMAKE_CXX_COMPILER   "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-g++.exe")
    set(CMAKE_LINKER         "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-ld.exe")
    set(CMAKE_SIZE           "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-size.exe")
    set(CMAKE_OBJCOPY        "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objcopy.exe")
    set(CMAKE_OBJDUMP        "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objdump.exe")
    set(RISCV_SIZE           "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-size.exe")
    set(RISCV_OBJCOPY        "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objcopy.exe")
    set(RISCV_OBJDUMP        "${MRS_TOOLCHAIN_PATH}/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-objdump.exe")
    set(RISCV_OPENOCD        "${MRS_TOOLCHAIN_PATH}/OpenOCD/bin/openocd.exe")
    set(RISCV_OPENOCD_CONFIG "${MRS_TOOLCHAIN_PATH}/OpenOCD/bin/wch-riscv.cfg")
else()
    message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()
# message(STATUS "Toolchain C Compiler: ${CMAKE_C_COMPILER}")
 
# as the elf is not stored verbatim into the microncontroller's flash, we can always have debug symbols included
set(CMAKE_ASM_FLAGS_INIT        "-ggdb3 -msmall-data-limit=8 -mno-save-restore -ffunction-sections -fdata-sections")
set(CMAKE_C_FLAGS_INIT          "-ggdb3 -msmall-data-limit=8 -mno-save-restore -ffunction-sections -fdata-sections")
set(CMAKE_CXX_FLAGS_INIT        "-ggdb3 -msmall-data-limit=8 -mno-save-restore -ffunction-sections -fdata-sections")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-ggdb3 -msmall-data-limit=8 -mno-save-restore -ffunction-sections -fdata-sections -nostartfiles -Xlinker --gc-sections --specs=nano.specs --specs=nosys.specs")
 
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)     # search headers
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)     # search libraries
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)     # search packages
set(CMAKE_FIND_ROOT_PATH_MODE_CMAKE   ONLY)     # search CMake files
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)    # search programs
 
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) # only compile, don't link

endif() # TOOLCHAIN_IS_INCLUDED